package com.itheima.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.User;
import com.itheima.service.UserService;
import com.itheima.utils.SMSUtils2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @PackageName: com.itheima.controller
 * @ClassName: UserController
 * @Description:
 * @Author: zhangcheng
 * @DateTime: 2022/6/12 14:30
 * @Version 1.0
 */

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送验证码
     *
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session) {
        String phone = user.getPhone();
        String code = RandomUtil.randomNumbers(6);//验证码
        /*SMSUtils2.sendSms(phone, code);*/
        //session.setAttribute(phone, code);
        //优化,缓存验证码
        redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);

        System.out.println(code);
        return R.success("验证码发送成功");
    }

    /**
     * 登录
     *
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<String> login(@RequestBody Map<String, String> map, HttpSession session) {
        //获取手机号和用户输入的验证码
        String phone = map.get("phone");
        String code = map.get("code");
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) {
            return R.error("手机号或验证码不能为空");
        }
        //从session中获取验证码,比对
        //String codeInSession = (String) session.getAttribute(phone);
        //优化
        Object codeInSession = redisTemplate.opsForValue().get(phone);

        if (code.equals(codeInSession)) {
            User one = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, phone));
            //如果手机号是没有注册的,自动保存到user表中
            if (one == null) {
                User user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            //登录成功,将用户数据存入session中
            session.setAttribute("user", one);
            return R.success("登陆成功");
        }
        //登陆成功,删除验证码
        redisTemplate.delete(phone);
        //登陆失败
        return R.error("登录失败");
    }

    /**
     * 退出
     *
     * @param session
     * @return
     */
    @PostMapping("/loginout")
    public R<String> loginout(HttpSession session) {
        session.removeAttribute("user");
        return R.success("退出成功");
    }

}
